﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DataFactory;
using EntityInterfaces;

namespace shopNet
{
    public class Cart
    {
        private Dictionary<IProduct, int> products;
        public Single cost {
            get {
                return products.Keys.Sum(p => (Single)p.price*products[p]);
            }
        }
        public Cart() {
            products = new Dictionary<IProduct, int>();           
        }
        public void Add(int index, int quantity) {
            var product = SimpleFactory.CreateProduct();
            product.Read(index);
            products.Add(product, quantity);
        }
        public void SetQuantity(int pID, int new_quantity){
            var product = SimpleFactory.CreateProduct();
            product.Read(pID);
            products[product] = new_quantity;
        }
        public void Delete(int index) {
            var product = SimpleFactory.CreateProduct();
            product.Read(index);            
            products.Remove(product);
        }
        public void Show() {
            foreach (var product in products.Keys) {
                Console.WriteLine("Name: {0}, Price: {1}, Quantity: {2}, Cost: {3}", product.name, product.price, products[product], products[product]*product.price);
            }
            Console.WriteLine("Total: {0}",this.cost);
        }
        public void Order(Customer cust) {
            var order = cust.Convert();
            order.order_time = DateTime.Now;
            order.Save();
            foreach (var prd in products.Keys) {
                order.AddItem(prd.id, products[prd]);
            }

        }
        public void Load(int id) {
            var order = SimpleFactory.CreateOrder();
            order.Read(id);
            products = order.GetItemsList();
        }
    }
}
